package com.gexiaobao.pigeon.ui.fragment.match

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.model.bean.RaceTypeResponse
import com.gexiaobao.pigeon.app.model.param.ProjectListParams
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentAppointMatchBinding
import com.gexiaobao.pigeon.ui.adapter.ProjectListAdapter
import com.gexiaobao.pigeon.ui.race.RaceViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 10:43
 * @Describe    : 指定的比赛列表
 */
class FragmentAppointMatch : BaseFragment<RaceViewModel, FragmentAppointMatchBinding>() {

    private val mAdapter: ProjectListAdapter by lazy { ProjectListAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>
    private var raceId: Int = 0

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose(getString(R.string.competition_events)) {
            nav().navigateUp()
        }
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context, 5f))
        )
    }

    override fun initData() {
        super.initData()
        raceId = requireArguments().getInt("raceId")
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getProjectList(ProjectListParams(raceId))
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getProjectList(ProjectListParams(raceId))
        }

        mAdapter.run {
            setOnItemClickListener { _, _, position ->
                val data = RaceTypeResponse.RaceTypeListBean(
                    mAdapter.data[position].aboutPassIds,
                    mAdapter.data[position].baseId,
                    mAdapter.data[position].projectId,
                    mAdapter.data[position].baseName,
                    mAdapter.data[position].baseStatus,
                    mAdapter.data[position].baseType,
                    mAdapter.data[position].endTime,
                    mAdapter.data[position].startTime
                )
                nav().navigateAction(R.id.action_to_specify_info_fragment, Bundle().apply {
                    putSerializable("data", data)
                })
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getProjectList(ProjectListParams(raceId))
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.appointRaceResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }
}